package com.sys.server.modules.controller;

import com.sys.server.common.annotation.Authentication;
import com.sys.server.common.annotation.Log;
import com.sys.server.common.model.R;
import com.sys.server.modules.controller.dto.CaseDto;
import com.sys.server.modules.controller.dto.PathDto;
import com.sys.server.modules.service.CaseService;
import com.sys.server.modules.service.FileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@Authentication
@Api(tags = "案例管理模块")
public class CaseController {

    private static final Logger LOG = LoggerFactory.getLogger(CaseController.class);
    @Autowired
    private CaseService caseService;

    @Autowired
    private FileService fileService;

    @RequestMapping(value = "/addCase", method = RequestMethod.POST)
    @ApiOperation(value = "案例管理模块--新增案例")
//    @Log(module = "案例管理", action = "新增案例", type = "新增")
    public R addCase(CaseDto caseDto) {
        return caseService.addCase(caseDto);
    }

    @RequestMapping(value = "/getCaseList", method = RequestMethod.GET)
    @ApiOperation(value = "案例管理模块--分页查询案例列表")
    public R queryCase(@RequestParam Long pagenum,
                       @RequestParam Long pagesize) {
        return caseService.queryCase(pagenum, pagesize);
    }

    @RequestMapping(value = "/getCaseInfoById", method = RequestMethod.GET)
    @ApiOperation(value = "案例管理模块--根据id查询案例信息")
    public R getCaseInfoById(@RequestParam Long cid) {
        return caseService.getCaseInfoById(cid);
    }

    @RequestMapping(value = "/downloadCaseFile", method = RequestMethod.POST)
    @ApiOperation(value = "案例管理模块--下载案例附件")
    public void downloadCaseFile(HttpServletResponse response, @RequestBody PathDto pathDto) throws IOException {
        LOG.info("number====" + pathDto.getPath());
        String path = caseService.downloadCaseFile(pathDto.getPath());
        LOG.info("path===" + path);
        fileService.fileDownload(response, path);
    }

    @RequestMapping(value = "/updateCaseInfo", method = RequestMethod.POST)
    @ApiOperation(value = "案例管理模块--更新案例信息")
    public R updateCaseInfo(@RequestBody CaseDto caseDto) {
        return caseService.updateCaseInfo(caseDto);
    }

    @RequestMapping(value = "/deleteCase", method = RequestMethod.POST)
    @ApiOperation(value = "案例管理模块--删除案例")
    @Log(module = "项目案例", action = "删除案例", type = "删除")
    public R deleteCase(@RequestBody CaseDto caseDto) {
        return caseService.deleteCase(caseDto.getCid());
    }

}
